package com.ptszyxx.popose.module.main.mine.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ptszyxx.popose.common.enums.ActionEnum;
import com.ptszyxx.popose.common.event.ExchangeDiamondEvent;
import com.ptszyxx.popose.common.utils.YActionEventUtil;
import com.ysg.bus.RxBus;
import com.ysg.bus.RxSubscriptions;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ysg.http.data.entity.mine.MineDiamondResult;
import com.ysg.http.data.entity.mine.MineWalletEntity;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.http.HttpUtils;
import com.ysg.http.data.entity.mine.PayEntity;
import com.ysg.utils.YChannelUtil;
import com.ysg.utils.YPhoneUtil;
import com.ysg.utils.YStringUtil;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class MineWalletVM extends BaseViewModel<CommonRepository> {
    public ObservableField<MineWalletEntity> entity = new ObservableField();
    public ObservableField<Boolean> privacyChecked = new ObservableField<>(false);
    public String orderInfo;   // 订单信息

    public MineWalletVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    @Override
    public void requestUploadAction() {
        YActionEventUtil.getInstance().upload(this, model, ActionEnum.mine_wallet);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onRefreshEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onPayDialogEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onAlipayEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onDiamondCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpMineDiamond(this));
    public BindingCommand onRechargeCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpMineRecharge(this));
    public BindingCommand onExchangeCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpMineExchange(this));
    public BindingCommand onCharmCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpMineCharm(this));
    public BindingCommand onPayCommand = new BindingCommand(()-> uc.onPayDialogEvent.call());
    public BindingCommand onRechargeAgreementCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpRechargeAgreement(this));

    //TODO 3.异步通知处理=====================
    public Disposable disposable;
    @Override
    public void registerRxBus() {
        super.registerRxBus();
        disposable = RxBus.getDefault().toObservable(ExchangeDiamondEvent.class)
                .subscribe(new Consumer<ExchangeDiamondEvent>() {
                    @Override
                    public void accept(ExchangeDiamondEvent event){
                        if (event != null){
                            requestWallet();
                        }
                    }
                });
    }

    @Override
    public void removeRxBus() {
        super.removeRxBus();
        RxSubscriptions.remove(disposable);
    }

    //=========================================接口：start===========================================
    /**
     * 钱包
     */
    public void requestWallet(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.wallet(map), this, (response)->{
                    entity.set((MineWalletEntity) response.getData());
                });
    }

    /**
     * 钻石列表
     */
    public void requestDiamond(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.diamond(map), this, new OnSuccessResult<MineDiamondResult>() {
                    @Override
                    public void onSuccessResult(BaseResponse<MineDiamondResult> response) {
                        uc.onRefreshEvent.setValue(response.getData().getList());
                    }
                });
    }

    /**
     * 支付
     */
    public void requestPay(String payType, String payId){
        Map<String, String> map = new HashMap<>();
        map.put("type", "1");
        map.put("projectId", payId);
        map.put("mobileSpec", YPhoneUtil.getModel());
        map.put("channel", YChannelUtil.getInstance().getChannel());

        Observable observable = model.payAlipay(map);
        if (YStringUtil.eq(2, payType)){
            observable = model.payWechat(map);
        }

        HttpUtils.getInstance()
                .data(observable, this, new OnSuccessResult<PayEntity>() {
                    @Override
                    public void onSuccessResult(BaseResponse<PayEntity> response) {
                        YActivityUtil.getInstance().jumpPay(MineWalletVM.this, response.getData().getPayUrl(), payType);
                    }
                });
    }
    /**
     * 支付
     */
    public void requestPayApp(String payType, String payId){
        Map<String, String> map = new HashMap<>();
        map.put("type", "1");
        map.put("projectId", payId);
        map.put("mobileSpec", YPhoneUtil.getModel());
        map.put("channel", YChannelUtil.getInstance().getChannel());

        HttpUtils.getInstance()
                .data(model.payAlipayApp(map), this, new OnSuccessResult<String>() {
                    @Override
                    public void onSuccessResult(BaseResponse<String> response) {
                        orderInfo = response.getData();
                        uc.onAlipayEvent.call();
                    }
                });
    }


}
